﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;

namespace Cocbook_DAL
{
    public static class DB
    {
        
        private static SqlCommand scmd;
        private static Connection con = new Connection();
        public static SqlConnection sc = new System.Data.SqlClient.SqlConnection(System.IO.File.ReadAllText("connectstring.dat"));
        

        public static void SetQuery(string queryString)
        {
            scmd = new SqlCommand(queryString,sc);
        }
        public static void AddParameters(String key, Object value)
        {
            scmd.Parameters.AddWithValue(key, value);
        }
        public static int ExeNoneQuery()
        {
            if (sc.State == System.Data.ConnectionState.Open) sc.Close();
            sc.Open();
            int q = scmd.ExecuteNonQuery();
            sc.Close();
            return q;
        }

        public static List<List<object>> ExeQuery()
        {
            SqlDataReader sdr = null;

            if (sc.State == System.Data.ConnectionState.Open) sc.Close();
            sc.Open();
            sdr = scmd.ExecuteReader();
            List<List<object>> result = new List<List<object>>();
            if (sdr.HasRows)
            {
                while (sdr.Read())
                {
                    List<object> lo = new List<object>();
                    for (int i = 0; i < sdr.FieldCount; i++)
                    {
                        lo.Add(sdr[i]);
                    }
                    result.Add(lo);
                }

            }
            sdr.Dispose();
            sc.Close();
            return result;
            
        }




    }
}
